﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace Entity
{
    public class SMDBContext : DbContext
    {
        private readonly string? _ConnectionString;


        public SMDBContext(DbContextOptions<SMDBContext> opts) : base(opts)
        {
        }
        /// <summary>
        /// 账户
        /// </summary>
        //public DbSet<EntityAccount> EntityAccount { get; set; }
        /// <summary>
        /// 用户信息
        /// </summary>
        public DbSet<EntityUser> EntityUser { get; set; }
        /// <summary>
        /// 报名表
        /// </summary>
        //public DbSet<EntityEntryForm> EntityEntryForm { get; set; }



        /// <summary>
        /// 程序链接数据库的账户密码配置
        /// </summary>
        /// <param name="optionsBuilder"></param>

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseMySql(_ConnectionString, new MySqlServerVersion(new Version(9, 2, 0)));
        //}


        /// <summary>
        /// 数据模型（表）构造函数
        ///1.设置表的一些字符集
        ///2.设定数据模型的·主外键等一些关系
        ///3.添加默认数据
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            EntityUser user = new EntityUser();
            user.FirstName = "123123";
            user.LastName = "3453";

            modelBuilder.Entity<EntityUser>().HasData([

                new EntityUser{
                    FirstName="zhangsan",
                    LastName="san",
                    Emai="4444sdfsdf@sdf.cvf"
                },

                new EntityUser{

                }
                ]);
        }
        /// <summary>
        /// 创建数据库时设定库和表的字符集（可以省略，创建库以后，表默认继承库的设置）
        /// </summary>
        /// <param name="modelBuilder"></param>
        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        // {
        //     modelBuilder.UseCollation("utf8mb4_german2_ci").HasCharSet("utf8mb4");
        // }
    }

    public class SMDBContextFactory : IDesignTimeDbContextFactory<SMDBContext>
    {
        public SMDBContext CreateDbContext(string[] args)
        {
            var optionBuilder = new DbContextOptionsBuilder<SMDBContext>();
            optionBuilder.UseMySql(
                 "server=localhost;userid=root;password=123456;database=AIYDB",
                new MySqlServerVersion(new Version(8, 0, 41))
                );

            return new SMDBContext(optionBuilder.Options);
        }
    }
}
